package com.atguigu.srb.core.service.impl;

import com.atguigu.srb.core.enums.TransTypeEnum;
import com.atguigu.srb.core.mapper.UserBindMapper;
import com.atguigu.srb.core.pojo.bo.TransFlowBO;
import com.atguigu.srb.core.pojo.entity.TransFlow;
import com.atguigu.srb.core.mapper.TransFlowMapper;
import com.atguigu.srb.core.pojo.entity.UserBind;
import com.atguigu.srb.core.service.TransFlowService;
import com.atguigu.srb.core.util.LendNoUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * <p>
 * 交易流水表 服务实现类
 * </p>
 *
 * @author fan
 * @since 2021-11-22
 */
@Service
public class TransFlowServiceImpl extends ServiceImpl<TransFlowMapper, TransFlow> implements TransFlowService {
    @Resource
    UserBindMapper userBindMapper;
    @Override
    public boolean isSaveTransFlow(String agentBillNo) {
        boolean b = false;
        QueryWrapper<TransFlow> transFlowQueryWrapper = new QueryWrapper<>();
        transFlowQueryWrapper.eq("trans_no",agentBillNo);
        Integer count = baseMapper.selectCount(transFlowQueryWrapper);
        if (count>0){
            b=true;
        }
        return b;
    }

    @Override
    public void saveTransFlow(TransFlowBO transFlowBO) {
        TransFlow transFlow = new TransFlow();
        String agentBillNo = LendNoUtils.getTransNo();
        BigDecimal amount = transFlowBO.getAmount();
        String bindCode = transFlowBO.getBindCode();
        String memo = transFlowBO.getMemo();
        TransTypeEnum transTypeEnum = transFlowBO.getTransTypeEnum();
        transFlow.setTransNo(agentBillNo);
        transFlow.setTransAmount(amount);
        transFlow.setMemo(memo);
        transFlow.setTransType(transTypeEnum.getTransType());

        QueryWrapper<UserBind> userBindQueryWrapper = new QueryWrapper<>();
        userBindQueryWrapper.eq("bind_code",bindCode);
        UserBind userBind = userBindMapper.selectOne(userBindQueryWrapper);

        transFlow.setUserName(userBind.getName());
        transFlow.setUserId(userBind.getUserId());
        transFlow.setTransTypeName(transTypeEnum.getTransTypeName());

        baseMapper.insert(transFlow);

    }
}
